package fr.utbm.gi.vi51.slizers.pw.gui.simulation;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import fr.utbm.gi.vi51.slizers.pw.WorldManager;

/**
 * Panel to display buttons for the simulation panel
 *
 * @author Adrien Hoffer
 *
 */
public class SimulationButtonsPanel extends JPanel
{

	/**
	 * Version number associated to the serializable class.
	 */
	private static final long	serialVersionUID	= -3091859441415826649L;
	
	/**
	 * The button to pause or resume simulation;
	 */
	private JButton pauseAndResumeSimulationBtn;
	
	/**
	 * The button to save creatures
	 */
	private JButton saveCreatures;
	
	/**
	 * Constructor
	 */
	public SimulationButtonsPanel()
	{
		this.pauseAndResumeSimulationBtn = new JButton( "Pause" );
		
		this.pauseAndResumeSimulationBtn.addActionListener( new ActionListener()
		{
			
			@Override
			public void actionPerformed( ActionEvent e )
			{
				if( WorldManager.isSimulationPlaying() )
				{
					WorldManager.pauseSimulation();
					SimulationButtonsPanel.this.pauseAndResumeSimulationBtn.setText( "Resume" );
					SimulationButtonsPanel.this.saveCreatures.setEnabled( true );
				}
				else
				{
					WorldManager.resumeSimulation();
					SimulationButtonsPanel.this.pauseAndResumeSimulationBtn.setText( "Pause" );
					SimulationButtonsPanel.this.saveCreatures.setEnabled( false );
				}
				
			}
		} );
		
		this.add( this.pauseAndResumeSimulationBtn, BorderLayout.NORTH );
		
		this.saveCreatures = new JButton("Save creatures");
		this.saveCreatures.setEnabled( false );
		
		// TODO et puis à griser tant que c'est pas en simulation pause
		this.saveCreatures.addActionListener( new ActionListener()
		{
			
			@Override
			public void actionPerformed( ActionEvent e )
			{
				String fileName = (String)JOptionPane.showInputDialog( "Please enter a file name for the creatures serialized file" );
				fileName = fileName.concat( ".slizers" );
				
				WorldManager.saveCreatures( fileName );
			}
		} );
		
		this.add( this.saveCreatures, BorderLayout.NORTH );
	}

}
